package com.mobile.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.mobile.aide.ui.R;
import com.mobile.debug.DebugTools;

public class NetImage{
	private final static String TAG = "NetImage";
	
	private NetImage() {
		
	}

    public static void setImage(final ImageView view, final String path) throws Exception{  
    	
    	new AsyncTask<String, Void, byte []>() {
			
			@Override
			protected byte [] doInBackground(String... params) {
				HttpURLConnection conn;
				InputStream inStream;
				byte[] urlData = null;
				
				try {
					URL url = new URL(params[0]); 
			        conn = (HttpURLConnection)url.openConnection();  
			        conn.setConnectTimeout(5 * 1000);  
			        conn.setRequestMethod("GET");  
			        inStream = conn.getInputStream(); 
			        
			        if(conn.getResponseCode()==200){  
			        	urlData = readStream(inStream);  
			        }else {
			        	urlData = null;
			        }
				}catch(MalformedURLException e) {
					DebugTools.getDebug().debug_e(TAG, "image down load url is wrong");
					e.printStackTrace();
				}catch(ProtocolException e) {
					DebugTools.getDebug().debug_e(TAG, "Protocol Exception");
					e.printStackTrace();
				}catch(IOException e) {
					DebugTools.getDebug().debug_e(TAG, "IO Exception");
					e.printStackTrace();
				}catch(Exception e) {
					DebugTools.getDebug().debug_e(TAG, "Exception");
					e.printStackTrace();
				}
				
				return urlData;
			}
			
			protected void onPostExecute(byte[] imageByte) {
				if(null != imageByte) {
	                try {  
	          
	                    if(null != imageByte){  
	                        Bitmap bitmap = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);// bitmap 
	                        
	                        if(null != bitmap) {
		                        view.setImageBitmap(bitmap);// display image  
		                        view.setScaleType(ImageView.ScaleType.FIT_XY);
	                        }else {
	                        	view.setImageResource(R.drawable.icon_yingxiao);
	                        }

	                    } 
	                } catch (Exception e) {  
	                	DebugTools.getDebug().debug_e(TAG, "Exception");  
	                    e.printStackTrace();  
	                } 
				}else {
					DebugTools.getDebug().debug_e(TAG, "Image data is null");
					return;
				}
			}

			protected void onCancelled() {}
    	}.execute(path, null, null);
    	
    }
    
    public static byte[] readStream(InputStream inStream) throws Exception{  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        while( (len=inStream.read(buffer)) != -1){  
            outStream.write(buffer, 0, len);  
        }  
        outStream.close();  
        inStream.close();  
        return outStream.toByteArray();  
    }  
}

